package br.ucb.sacop.activities;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import net.objecthunter.exp4j.ExpressionBuilder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import br.ucb.sacop.R;
import br.ucb.sacop.bean.TurmaShort;
import br.ucb.sacop.util.AvaliacoesSingleton;
import br.ucb.sacop.webservice.TurmaREST;

public class CalculaMediaActivity extends Activity{
	
	private List<String> avaliacoes;
	private String formula;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calcula_media);
		Bundle extras = getIntent().getExtras();
		if(extras!=null){
			TurmaShort ts = (TurmaShort) extras.get("turmaShort");
			TurmaREST turmaREST = new TurmaREST();
			try {
				this.formula = turmaREST.getFormulaByTurma(ts);
				defineAvaliacoes(this.formula);
			} catch (Exception e) {
				e.printStackTrace();
				gerarToast("Erro ao capturar Turmas!");
				finish(); //close activity!!!
			}
			ArrayAdapter<String> adapterTurmas = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
					this.avaliacoes);
				Spinner spiAvaliacoes;
				spiAvaliacoes = (Spinner) findViewById(R.id.variaveis);
				spiAvaliacoes.setAdapter(adapterTurmas);
		}else{
			gerarToast("falha ao recuperar turma!");
			finish(); //close activity!!!
		}
		Button btInserirNota = (Button) findViewById(R.id.inserirNota);
		btInserirNota.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Spinner variaveis = (Spinner) findViewById(R.id.variaveis);
				Intent intent = new Intent();
				intent.setClass(CalculaMediaActivity.this, InsereNotaActivity.class);
				intent.putExtra("variavel", avaliacoes.get(variaveis.getSelectedItemPosition()));
				startActivity(intent);
			}
		});
		
		Button btCalculaMedia = (Button) findViewById(R.id.calcularMedia);
		btCalculaMedia.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(AvaliacoesSingleton.getInstance().size() == avaliacoes.size()){
					TextView media = (TextView) findViewById(R.id.media);
					Double resultado = new ExpressionBuilder(substituiValores(formula)).build().evaluate();
					media.setText(resultado.toString());
					media.setVisibility(View.VISIBLE);
				}else{
					gerarToast("insira todas as notas primeiro!");
				}
			}
		});
		
	}
	
	
	private String substituiValores(String expressao) {
		if (!AvaliacoesSingleton.getInstance().isEmpty()) {
			for (Entry<String, Double> entry : AvaliacoesSingleton.getInstance().entrySet()) {
				expressao = expressao.toUpperCase().replace(entry.getKey(), entry.getValue().toString());
			}
		}
		
		return expressao;
	}
	
	public void defineAvaliacoes(String formula) {
		this.avaliacoes = new ArrayList<String>();
		int contProva = 0;
		int contTrabalho = 0;
		for (int i = 0; i < formula.length(); i++) {
			if (formula.toUpperCase().charAt(i) == 'P') {
				contProva++;
				this.avaliacoes.add("P" + contProva);
			}

		}
		for (int i = 0; i < formula.length(); i++) {
			if (formula.toUpperCase().charAt(i) == 'T') {
				contTrabalho++;
				this.avaliacoes.add("T" + contTrabalho);
			}

		}
	}

	private void gerarToast(CharSequence message) {
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast
				.makeText(getApplicationContext(), message, duration);
		toast.show();
	}
}
